package com.sbl.bajw.pc.service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sbl.bajw.common.CoordConver;
import com.sbl.bajw.mapper.PolicePositionDao;
import com.sbl.bajw.pc.model.PolicePositionModel;

@Service
public class PolicePositionService {
	
	@Resource
    private PolicePositionDao dao;
	
	public List<PolicePositionModel> getPositionByOrgIds(HashMap<String, Object> ids) throws Exception{
		List<PolicePositionModel> models = dao.getPositionByOrgIds(ids);
		return models;
	}
	
	public List<HashMap<String, Object>> getPoition(HashMap<String, Object> parm){
		List<HashMap<String, Object>> models = dao.getPoition(parm);
		for(int i=0;i<models.size();i++){
			HashMap<String, Object> model = models.get(i);
			String xStr = model.get("X").toString();
			String yStr = model.get("Y").toString();
			double x = Double.parseDouble(xStr);
			double y = Double.parseDouble(yStr);
			double[] xyConvered = CoordConver.converWgs84ToDb09(x, y);
			model.put("X", xyConvered[0]+"");
			model.put("Y", xyConvered[1]+"");
		}
		return models;
	}
	
	public List<HashMap<String, Object>> getHistoryPoition(HashMap<String, Object> parm){
		List<HashMap<String, Object>> models = dao.getHistoryPoition(parm);
		for(int i=0;i<models.size();i++){
			HashMap<String, Object> model = models.get(i);
			String xStr = model.get("X").toString();
			String yStr = model.get("Y").toString();
			double x = Double.parseDouble(xStr);
			double y = Double.parseDouble(yStr);
			double[] xyConvered = CoordConver.converWgs84ToDb09(x, y);
			model.put("X", xyConvered[0]+"");
			model.put("Y", xyConvered[1]+"");
		}
		return models;
	}
	
	public List<HashMap<String, Object>> getCurrentPoition(){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, -3);
		Date date = cal.getTime();
		parm.put("time", date);
		List<HashMap<String, Object>> models = dao.getPoition(parm);
		return models;
	}
	
	public PolicePositionModel getModelNo(String no){
		PolicePositionModel model = dao.getModelNo(no);
		String xStr = model.getX().toString();
		String yStr = model.getY().toString();
		double x = Double.parseDouble(xStr);
		double y = Double.parseDouble(yStr);
		double[] xyConvered = CoordConver.converWgs84ToDb09(x, y);
		model.setX(new BigDecimal(xyConvered[0]));
		model.setY(new BigDecimal(xyConvered[1]));
		return model;
	}
	
	public List<HashMap<String, String>> getCenterCount(HashMap<String, Object> parm){
		List<HashMap<String, String>> result = dao.getCenterCount(parm);
		return result;
	}
	
	public List<HashMap<String, String>> StatisticsByOrgid(HashMap<String, Object> parm){
		List<HashMap<String, String>> models = dao.StatisticsByOrgid(parm);
		return models;
	}
}
